﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Manager.DataEntity;
using ERP.Manager.DataProvider;
using ERP.Manager.DataHelper;
using System.Data.SqlClient;
using System.Data;
using ERP.Manager.Core;


namespace ERP.Manager.SQLServerProvider
{
    public class MessageRepository : IMessageRepository
    {

        #region 添加公告信息的方法
        public int AddMessage(Messageinfo message)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.Add_MESSAGE,
               new SqlParameter("@messageTitle", message.messageTitle),
               new SqlParameter("@messageContent", message.messageContent),
               new SqlParameter("@fromUserId", message.UserInfo.userId),
               new SqlParameter("@publishDate", message.publishDate));
            return rowcount;
        }
        #endregion

        #region 删除公告信息的方法
        public int DeleteMessage(int messageId)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.DELETE_MESSAGE, new SqlParameter("@messageId", messageId));
            return rowcount;
        }
        #endregion

        #region 修改公告信息的方法
        public int UpdateMessage(Messageinfo message)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.UPDATE_MESSAGE,
                new SqlParameter("@messagetitle", message.messageTitle),
                new SqlParameter("@messagecontent", message.messageContent),
                 new SqlParameter("@messageid", message.messageId));
            return rowcount;
        }
        #endregion

        #region 查询最新动态的头五条公告信息
        public List<Messageinfo> getMessagesToIndex()
        {
            List<Messageinfo> list = new List<Messageinfo>();
            Messageinfo message = null;
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.GET_MESSAGE_TOINDEX);
            try
            {
                while (read.Read())
                {
                    message = new Messageinfo();
                    message.messageId = int.Parse(read["messageId"].ToString());
                    message.messageTitle = read["messageTitle"].ToString();
                    message.messageContent = read["messageContent"].ToString();
                    message.UserInfo.userId = read["fromUserId"].ToString();
                    message.publishDate = DateTime.Parse(read["publishDate"].ToString());
                    list.Add(message);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 分页显示公告信息
        public List<Messageinfo> getAllMessages(int pagecount)
        {
            List<Messageinfo> list = new List<Messageinfo>();
            Messageinfo message = null;
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_MESSAGE_PAGE, new SqlParameter("@pagecount", pagecount));
            try
            {
                while (read.Read())
                {
                    message = new Messageinfo();
                    message.messageId = int.Parse(read["messageId"].ToString());
                    message.messageTitle = read["messageTitle"].ToString();
                    message.messageContent = read["messageContent"].ToString();
                    message.UserInfo.userId = read["fromUserId"].ToString();
                    message.UserInfo.userName = read["username"].ToString();
                    message.DepartmentInfo.departmentId = read["departmentId"].ToString();
                    message.DepartmentInfo.departmentName = read["departmentName"].ToString();
                    message.publishDate = DateTime.Parse(read["publishDate"].ToString());
                    list.Add(message);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion      

        #region 查询公告详细信息
        public Messageinfo AllMessageDetail(Messageinfo message)
        {
            Messageinfo messages = null;
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_MESSAGE_DETAIL, new SqlParameter("@messageid", message.messageId));
            try
            {
                while (read.Read())
                {
                    messages = new Messageinfo();
                    messages.messageId = int.Parse(read["messageId"].ToString());
                    messages.messageTitle = read["messageTitle"].ToString();
                    messages.messageContent = read["messageContent"].ToString();
                    messages.UserInfo.userId = read["fromUserId"].ToString();
                    messages.UserInfo.userName = read["username"].ToString();
                    messages.DepartmentInfo.departmentId = read["departmentId"].ToString();
                    messages.DepartmentInfo.departmentName = read["departmentName"].ToString();
                    messages.publishDate = DateTime.Parse(read["publishDate"].ToString());
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return messages;
        }
        #endregion       

        #region 得到所有公告条数信息的方法
        public int getAllMessageCount()
        {
            var result = SQLHelper.ExecuteScalar(CommandType.Text, ERPSQL.ALL_MESSAGE_PAGE_COUNT);
            return int.Parse(result.ToString());
        }
        #endregion

        #region 根据用户编号查询公告信息
        public List<Messageinfo> getallmessagefromuserid(string fromuserid)
        {
            List<Messageinfo> list = new List<Messageinfo>();
            Messageinfo message = null;
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.GETALL_MESSAGE_FROMUSERID, new SqlParameter("@fromuserid", fromuserid));
            try
            {
                while (read.Read())
                {
                    message = new Messageinfo();
                    message.messageId = int.Parse(read["messageId"].ToString());
                    message.messageTitle = read["messageTitle"].ToString();
                    message.messageContent = read["messageContent"].ToString();
                    message.UserInfo.userId = read["fromUserId"].ToString();
                    message.publishDate = DateTime.Parse(read["publishDate"].ToString());
                    list.Add(message);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 根据用户编号删除公告信息
        public int deletemessagefromuserid(string fromuserid)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.UPDATE_MESSAGE, new SqlParameter("@fromuserid", fromuserid));
            return rowcount;
        }
        #endregion
    }
}
